<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="类别名称" style="width: 150px;" class="filter-item" />
      <el-button :disabled="!(this.listQuery.name && this.listQuery.name !== '')" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-refresh-left" @click="handleReset">
        重置
      </el-button>
      <el-button class="filter-item fr" type="warning" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column prop="id" label="类别编号" />
      <el-table-column prop="name" label="类别名称" />
      <el-table-column label="创建时间">
        <template slot-scope="{ row }">
          <span>{{ new Date(row.create_time) | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.$index, scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>8"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="outerVisible">
      <el-form :model="info">
        <el-form-item label="类别名称" :label-width="formLabelWidth">
          <el-input v-model="info.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="update"
        >确 定
        </el-button>
      </div>
    </el-dialog>

    <!-- 添加 -->
    <el-dialog title="添加" :visible.sync="addVisible">
      <el-form :model="info">
        <el-form-item label="类别名称" :label-width="formLabelWidth">
          <el-input v-model="info.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="add"
        >确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { types, addType, delType, updateType } from '@/api'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'

export default {
  name: 'Type',
  components: {
    Pagination
  },
  data() {
    return {
      parseTime: parseTime,
      listQuery: {
        name: '', // 类别名称
        page: 1,
        limit: 8
      },
      formLabelWidth: '120px',
      info: {
        // 绑定模态框属性
        id: '',
        name: ''
      },
      addVisible: false, // 控制新增
      outerVisible: false, // 控制编辑
      tableData: [],
      total: 0
    }
  },
  created() {
    this.fetchAll()
  },
  methods: {
    // 筛选
    handleFilter() {
      console.log('筛选')
    },
    // 重置
    handleReset() {
      this.listQuery.name = ''
      this.fetchAll()
    },
    // 点击新增
    handleCreate() {
      this.addVisible = true
    },
    // 新增
    add() {
      addType({ payType: this.info.name }).then(() => {
        this.addVisible = false
        this.fetchAll()
      })
    },
    // 获取分类列表
    fetchAll() {
      const { page, limit } = this.listQuery
      types({ currentPage: page, pagesize: limit }).then(res => {
        this.tableData = res.data
      })
    },
    // 编辑行
    handleEdit(index, row) {
      // 给模态窗属性填值
      this.info.id = row.id
      this.info.name = row.name
      this.info.create_time = row.create_time
      // 显示模态窗体
      this.outerVisible = true
    },
    // 修改
    update() {
      updateType({ id: this.info.id, payType: this.info.name }).then(() => {
        this.fetchAll()
        this.outerVisible = false
      })
    },
    // 删除行
    handleDelete(index, row) {
      console.log(index, row)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          delType({ id: row.id }).then(() => {
            this.fetchAll()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分页事件
    getList() {
      console.log('分页事件')
      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //
      //   // Just to simulate the time of the request
      //   setTimeout(() => {
      //     this.listLoading = false
      //   }, 1.5 * 1000)
      // })
    }
  }
}
</script>
